package com.spic.system.service.impl;

import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.system.api.RemoteFileService;
import com.spic.system.domain.SysAnnounceReader;
import com.spic.system.domain.vo.SysAnnounceReaderVo;
import com.spic.system.enums.AnnounceDayTypeEnum;
import com.spic.system.mapper.SysAnnounceReaderMapper;
import com.spic.system.service.ISysAnnounceReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 阅读人Service业务层处理
 *
 * @author spic
 * @date 2022-06-15
 */
@Service
public class SysAnnounceReaderServiceImpl implements ISysAnnounceReaderService
{
    @Autowired
    private SysAnnounceReaderMapper sysAnnounceReaderMapper;
    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询阅读人
     *
     * @param id 阅读人主键
     * @return 阅读人
     */
    @Override
    public SysAnnounceReader selectSysAnnounceReaderById(Long id)
    {
        return sysAnnounceReaderMapper.selectSysAnnounceReaderById(id);
    }

    /**
     * 查询阅读人列表
     *
     * @param sysAnnounceReader 阅读人
     * @return 阅读人
     */
    @Override
    public List<SysAnnounceReader> selectSysAnnounceReaderList(SysAnnounceReader sysAnnounceReader)
    {
        return sysAnnounceReaderMapper.selectSysAnnounceReaderList(sysAnnounceReader);
    }

    /**
     * 新增阅读人
     *
     * @param sysAnnounceReader 阅读人
     * @return 结果
     */
    @Override
    public int insertSysAnnounceReader(SysAnnounceReader sysAnnounceReader)
    {
        return sysAnnounceReaderMapper.insertSysAnnounceReader(sysAnnounceReader);
    }

    /**
     * 修改阅读人
     *
     * @param sysAnnounceReader 阅读人
     * @return 结果
     */
    @Override
    public int updateSysAnnounceReader(SysAnnounceReader sysAnnounceReader)
    {
        return sysAnnounceReaderMapper.updateSysAnnounceReader(sysAnnounceReader);
    }

    /**
     * 批量删除阅读人
     *
     * @param ids 需要删除的阅读人主键
     * @return 结果
     */
    @Override
    public int deleteSysAnnounceReaderByIds(Long[] ids)
    {
        return sysAnnounceReaderMapper.deleteSysAnnounceReaderByIds(ids);
    }

    /**
     * 删除阅读人信息
     *
     * @param id 阅读人主键
     * @return 结果
     */
    @Override
    public int deleteSysAnnounceReaderById(Long id)
    {
        return sysAnnounceReaderMapper.deleteSysAnnounceReaderById(id);
    }

    @Override
    public List<SysAnnounceReaderVo> selectSysAnnounceReaderListByUserId(SysAnnounceReaderVo sysAnnounceReaderVo)
    {
        int dayNumber =Integer.parseInt(AnnounceDayTypeEnum.MONTH_TYPE_NUMBER.getCode());
        String type = sysAnnounceReaderVo.getType();
        Long userId = SecurityUtils.getUserId();
        String dayType = sysAnnounceReaderVo.getDayType();
        if(AnnounceDayTypeEnum.MONTH_TYPE.getCode().equals(dayType)){ //一个月
            dayNumber =Integer.parseInt(AnnounceDayTypeEnum.MONTH_TYPE_NUMBER.getCode());
        }else if(AnnounceDayTypeEnum.THREE_MONTH.getCode().equals(dayType)){ //三个月
            dayNumber = Integer.parseInt(AnnounceDayTypeEnum.THREE_MONTH_NUMBER.getCode());
        }else if(AnnounceDayTypeEnum.ONE_YEAR.getCode().equals(dayType)){ //一年
            dayNumber= Integer.parseInt(AnnounceDayTypeEnum.ONE_YEAR_NUMBER.getCode());
        }
        List<SysAnnounceReaderVo>  sysAnnounceReaderVoList = sysAnnounceReaderMapper.selectSysAnnounceReaderListByUserId(userId,type,dayNumber);
        return sysAnnounceReaderVoList;
    }

    @Override
    public void updateSysAnnounceReaderByStateHidden()
    {
        Date nowDate = DateUtils.getNowDate();
        SysAnnounceReader reader = new SysAnnounceReader();
        reader.setState("0");
        List<SysAnnounceReader> sysAnnounceReaders = sysAnnounceReaderMapper.selectSysAnnounceReaderList(reader);
        for (SysAnnounceReader sysAnnounceReader : sysAnnounceReaders)
        {
            Date endTime = sysAnnounceReader.getEndTime();
            SysAnnounceReader re = new SysAnnounceReader();
            re.setId(sysAnnounceReader.getId());
            re.setState("1");
            if(nowDate.compareTo(endTime)>=0){
                sysAnnounceReaderMapper.updateSysAnnounceReader(re) ;
            }
        }
    }

    /**
     * 按照当前用户和类型 查询 消息列表
     * @param sysAnnounceReaderVo
     * @return
     */
    @Override
    public List<SysAnnounceReaderVo> getAnnounceReaderByTypeList(SysAnnounceReaderVo sysAnnounceReaderVo)
    {
        List<SysAnnounceReaderVo> list = new ArrayList<>();
        String type = sysAnnounceReaderVo.getType();
        Long userId = SecurityUtils.getUserId();
        //部门级
        List<SysAnnounceReaderVo> sysAnnounceReaderVoList = sysAnnounceReaderMapper.selectAnnounceReaderByTypeList(userId, type);
        //矿级
        List<SysAnnounceReaderVo> sysAnnounceReaderVoList2 = sysAnnounceReaderMapper.selectAnnounceReaderByAnnounceIdList(type);

        if(sysAnnounceReaderVoList!=null && sysAnnounceReaderVoList.size()>0){
            list.addAll(sysAnnounceReaderVoList);
        }

        if(sysAnnounceReaderVoList2!=null && sysAnnounceReaderVoList2.size()>0){
            list.addAll(sysAnnounceReaderVoList2);
        }

        String regulatoryType = sysAnnounceReaderVo.getRegulatoryType();
        if (StringUtils.isNotEmpty(regulatoryType)) {
            list = list.stream().filter(item -> regulatoryType.equals(item.getRegulatoryType())).collect(Collectors.toList());
        }

        //改为迭代器写法, 避免之前lambda表达式写法的: Variable used in lambda expression should be final or effectively final
        Iterator<SysAnnounceReaderVo> iterator = list.iterator();
        //广告机部门简介没有文件
        if(!"6".equals(type)) {
            while (iterator.hasNext()) {
                SysAnnounceReaderVo item = iterator.next();
                String file = item.getFile();
                String strFile = file.substring(0, file.lastIndexOf("."));
                String str2 = file.substring(strFile.length()+1, file.length());
                item.setFileType(str2);
                String transformedUrl = file.substring(0, file.indexOf("|"));
                String msg = remoteFileService.getSysFileRecordByOriginalUrl(transformedUrl).getMsg();
                if(StringUtils.isNotEmpty(msg)){
                    item.setFile(msg);
                } else{
                    iterator.remove();
                }
            }
        }
        Collections.sort(list);
        return list;
    }

    // 查询 当前用户下 类型为安全公示类别的 部门级和矿级 数据并聚合
    @Override
    public List<SysAnnounceReaderVo> getAnnounceReaderBySafetyList()
    {
        String type = "0";  //公示公告类型
        Long userId = SecurityUtils.getUserId();
        List<SysAnnounceReaderVo> list = new ArrayList<>();

        //部门级
        List<SysAnnounceReaderVo> sysAnnounceReaderVoList1 = sysAnnounceReaderMapper.selectAnnounceReaderByTypeList(userId, type);
        //矿级
        List<SysAnnounceReaderVo> sysAnnounceReaderVoList2 = sysAnnounceReaderMapper.selectAnnounceReaderByAnnounceIdList(type);

        if(sysAnnounceReaderVoList1!=null && sysAnnounceReaderVoList1.size()>0){
            list.addAll(sysAnnounceReaderVoList1);
        }
        if(sysAnnounceReaderVoList2!=null && sysAnnounceReaderVoList2.size()>0){
            list.addAll(sysAnnounceReaderVoList2);
        }
        List<SysAnnounceReaderVo> collect = list.stream().sorted(Comparator.comparing(SysAnnounceReaderVo::getReleaseType)).collect(Collectors.toList());
        collect.forEach(item->{
            String file = item.getFile();

            String strFile = file.substring(0, file.lastIndexOf("."));
            String str2 = file.substring(strFile.length()+1, file.length());
            item.setFileType(str2);

            String transformedUrl = file.substring(0, file.indexOf("|"));
            String msg = remoteFileService.getSysFileRecordByOriginalUrl(transformedUrl).getMsg();

            if(StringUtils.isNotEmpty(msg)){
                item.setFile(msg);
            } else{
                collect.remove(item);
            }
        });
        return collect;
    }

    public static void main(String[] args) throws ParseException
    {
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = DateUtils.getNowDate();
        Date date2 = sdf.parse("2029-12-31 12:00:01");
        System.out.println("date1 : " + sdf.format(date1));
        System.out.println("date2 : " + sdf.format(date2));
        int i = date1.compareTo(date2);
        System.out.println(i);*/


    }
}
